<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="jquery.js"></script>
    <script src="sisyphus.min.js"></script>
</head>
<body>
<h2>设置请求属性</h2>
<style>
    .w {
        width: 100%;;
    }

</style>
<form id="qst">
    ecode:<input name="ecode" id="ecode" class="w" value=""/> <br/>
    sid:<input name="sid" id="sid" class="w" value=""/> <br/>
    uid:<input name="userid" id="userid" class="w" value=""/> <br/>
    cookieStr:<textarea name="cookieStr" id="cookieStr" class="w"></textarea> <br/>
    商品id:<input name="spid" id="spid" class="w" value=""/> <br/>
    skuId:<input name="skuId" id="skuId" class="w" value=""/> <br/>
</form>
<input id="start_btn" type="button" value="启动">

<div style="margin: 0 auto;text-align: center">
    <div id="clock"></div>
    <img id="qst_img" width="458" height="100">

    <div>
        <input name="answer" id="answer"> <input name="aaaaanswer" id="ok" type="button" value="确定">
    </div>
    <div id="showStatus"></div>
</div>
<script>
    var qstData;
    $(document).ready(function () {
        $("#start_btn").click(function () {
            setTimeout(startQst, 150);
        });
        $("#ok").click(function () {
            commitAnswer();
        });
        $("#answer").keydown(function (e) {
            var currKey = 0, e = e || event;
            if (e.keyCode == 13) {
                commitAnswer();
            }
        });

        //定时保存
        $("#qst").sisyphus();
    });
    function startQst() {
        var url = "http://ax.m.taobao.com/qst.htm?f=w&id=" + $("#spid").val() + "&uid=" + $("#userid").val() + "&cb=&r=" + new Date().getTime();
        $.ajax({
            url: "qst",
            type: "get",
            data: {
                "type": "qst",
                "cookieStr": $("#cookieStr").val(),
                "url": url
            },
            dataType: "json",
            success: function (data) {
                if (data.status) {
//                {"lgin":true,"sign":"","qst":"","stock":0,"now":1408006799909,"tip":"","len":0}
                    var result = data.data;
                    var showTime = new Date(result.now).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
                    $("#clock").html(showTime);
                    if (result.qst != "") {
                        $("#qst_img").attr("src", result.qst);
                        $("#answer").focus();
                        return;
                    }
                } else {
                    console.error(data.msg);
                    $("#showStatus").html(data.msg);
                }
                setTimeout(startQst, 80);
            }
        });

    }
    function commitAnswer() {
        var answer = $("#answer").val();
        if (answer == "") {
            $("#showStatus").html("可能得再点击下！！！");
            return;
        }
        //提交
        var url = "http://ax.m.taobao.com/stock2.htm?f=w&id=" + $("#spid").val() + "&uid=" + $("#userid").val() + "&skuId=" + $("#skuId").val() + "&r=" + new Date().getTime();
        $.ajax({
            url: "qst",
            type: "get",
            data: {
                "type": "stock2",
                "cookieStr": $("#cookieStr").val(),
                "url": url,
                "answer": answer,
                "itemId": $("#spid").val(),
                "skuId": $("#skuId").val(),
                "appKey": $("#appkey").val(),
                "sid": $("#sid").val(),
                "ecode": $("#ecode").val()
            },
            dataType: "json",
            success: function (data) {
                if (data.status) {
//                        {"lgin":true,"stock":0,"sku":0,"now":1408006804970,"timkn":"","timk":""}
                    console.info(data.data);
                    $("#showStatus").html(data.data);
                } else {
                    console.error(data.msg);
                    $("#showStatus").html(data.msg);
                }
            }
        });
        $("#showStatus").html("已经提交");
    }

</script>
</body>
</html>